package cn.itsource.pethome.base.service.impl;

import cn.itsource.pethome.base.mapper.BaseMapper;
import cn.itsource.pethome.base.service.IBaseService;
import cn.itsource.pethome.base.query.BaseQuery;
import cn.itsource.pethome.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public PageInfo<T> findPageByQuery(BaseQuery tQuery) {
        //总条数
        Long total = mapper.findCount(tQuery);
        //如果总条数为0，就没必要查询分页列表了
        if(total.equals(0L)){
            return new PageInfo<T>();
        }
        //查询分页列表
        List<T> list = mapper.findListByQuery(tQuery);
        //把总条数和分页列表，封装成PageInfo对象
        return new PageInfo<T>(list, total);
    }

    @Override
    @Transactional
    public void batchRemove(List<T> ts) {
        mapper.batchRemove(ts);
    }
}
